package com.platform.usercenter.service.impl;

import com.platform.common.core.utils.DateUtils;
import com.platform.common.core.utils.SpringUtils;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.datascope.annotation.DataScope;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.SysOrganizationDept;
import com.platform.usercenter.domain.vo.TreeSelect;
import com.platform.usercenter.mapper.SysOrganizationDeptMapper;
import com.platform.usercenter.service.ISysOrganizationDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织部门Service业务层处理
 *
 * @author platform
 * @date 2024-10-24
 */
@Service
public class SysOrganizationDeptServiceImpl implements ISysOrganizationDeptService {
    @Resource
    private SysOrganizationDeptMapper sysOrganizationDeptMapper;

    /**
     * 查询组织部门
     *
     * @param deptId 组织部门主键
     * @return 组织部门
     */
    @Override
    public SysOrganizationDept selectSysOrganizationDeptByDeptId(Long deptId) {
        return sysOrganizationDeptMapper.selectSysOrganizationDeptByDeptId(deptId);
    }

    /**
     * 查询组织部门列表
     *
     * @param sysOrganizationDept 组织部门
     * @return 组织部门
     */
    @Override
    public List<SysOrganizationDept> selectSysOrganizationDeptList(SysOrganizationDept sysOrganizationDept) {
        return sysOrganizationDeptMapper.selectSysOrganizationDeptList(sysOrganizationDept);
    }

    /**
     * 新增组织部门
     *
     * @param sysOrganizationDept 组织部门
     * @return 结果
     */
    @Override
    public int insertSysOrganizationDept(SysOrganizationDept sysOrganizationDept) {
        sysOrganizationDept.setCreateTime(DateUtils.getNowDate());
        return sysOrganizationDeptMapper.insertSysOrganizationDept(sysOrganizationDept);
    }

    /**
     * 修改组织部门
     *
     * @param sysOrganizationDept 组织部门
     * @return 结果
     */
    @Override
    public int updateSysOrganizationDept(SysOrganizationDept sysOrganizationDept) {
        sysOrganizationDept.setUpdateTime(DateUtils.getNowDate());
        return sysOrganizationDeptMapper.updateSysOrganizationDept(sysOrganizationDept);
    }

    /**
     * 批量删除组织部门
     *
     * @param deptIds 需要删除的组织部门主键
     * @return 结果
     */
    @Override
    public int deleteSysOrganizationDeptByDeptIds(Long[] deptIds) {
        return sysOrganizationDeptMapper.deleteSysOrganizationDeptByDeptIds(deptIds);
    }

    /**
     * 删除组织部门信息
     *
     * @param deptId 组织部门主键
     * @return 结果
     */
    @Override
    public int deleteSysOrganizationDeptByDeptId(Long deptId) {
        return sysOrganizationDeptMapper.deleteSysOrganizationDeptByDeptId(deptId);
    }

    @Override
    public List<TreeSelect> selectDeptTreeList4Admin(SysOrganizationDept dept) {
        List<SysOrganizationDept> deptList = sysOrganizationDeptMapper.selectDeptList(dept);
        return buildDeptTreeSelect(deptList);
    }

    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectDeptTreeList(SysOrganizationDept dept) {
        List<SysOrganizationDept> deptList = SpringUtils.getAopProxy(this).selectDeptList(dept);
        return buildDeptTreeSelect(deptList);
    }

    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> noPermissionsDeptTree(SysOrganizationDept dept) {
        List<SysOrganizationDept> deptList = SpringUtils.getAopProxy(this).selectNoPermissionsDeptTree(dept);
        return buildDeptTreeSelect(deptList);
    }


    @Override
    public List<TreeSelect> selectUserAllDeptTree() {
        Long userId = SecurityUtils.getUserId();
        List<SysOrganizationDept> deptList = sysOrganizationDeptMapper.selectDeptListByUserId(userId);
        return buildDeptTreeSelect(deptList);
    }

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<SysOrganizationDept> selectDeptList(SysOrganizationDept dept) {
        return sysOrganizationDeptMapper.selectDeptList(dept);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param deptList 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysOrganizationDept> deptList) {
        List<SysOrganizationDept> deptTrees = buildDeptTree(deptList);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SysOrganizationDept> buildDeptTree(List<SysOrganizationDept> deptList) {
        List<SysOrganizationDept> returnList = new ArrayList<>();
        List<Long> tempList = deptList.stream().map(SysOrganizationDept::getDeptId).collect(Collectors.toList());
        for (SysOrganizationDept dept : deptList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(deptList, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = deptList;
        }
        return returnList;
    }

    /**
     * 查询全部部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectAllDeptList(SysOrganizationDept dept) {
        List<SysOrganizationDept> deptList = sysOrganizationDeptMapper.selectAllDeptList(dept);
        return buildDeptTreeSelect(deptList);
    }

    @Override
    public List<TreeSelect> selectCurrentOrgDeptList(SysOrganizationDept dept) {
        dept.setOrgId(SecurityUtils.getLoginUser().getOrgId());
        List<SysOrganizationDept> deptList = sysOrganizationDeptMapper.selectDeptList(dept);
        return buildDeptTreeSelect(deptList);
    }

    /**
     * 小程序
     *
     * @return
     */
    @Override
    public List<TreeSelect> selectUserAllDept() {
        Long userId = SecurityUtils.getUserId();
        List<SysOrganizationDept> deptList = sysOrganizationDeptMapper.selectDeptListByUserId(userId);
        return deptList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    public List<SysOrganizationDept> selectNoPermissionsDeptTree(SysOrganizationDept dept) {
        return sysOrganizationDeptMapper.selectDeptList(dept);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysOrganizationDept> list, SysOrganizationDept t) {
        // 得到子节点列表
        List<SysOrganizationDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysOrganizationDept tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysOrganizationDept> getChildList(List<SysOrganizationDept> list, SysOrganizationDept t) {
        List<SysOrganizationDept> tlist = new ArrayList<>();
        for (SysOrganizationDept n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysOrganizationDept> list, SysOrganizationDept t) {
        return getChildList(list, t).size() > 0;
    }
}
